package cn.edu.suse.epilepsy.service.impl;

import cn.edu.suse.epilepsy.entity.vo.SysUserVo;
import cn.edu.suse.epilepsy.util.BCrypUtil;
import cn.edu.suse.epilepsy.common.BizException;
import cn.edu.suse.epilepsy.entity.dto.SysUserLoginDto;
import cn.edu.suse.epilepsy.entity.dto.SysUserRegisterDto;
import cn.edu.suse.epilepsy.entity.pojo.SysUser;
import cn.edu.suse.epilepsy.mapper.SysUserMapper;
import cn.edu.suse.epilepsy.service.SysUserService;
import cn.edu.suse.epilepsy.util.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @Author: QingShu
 * @Date: 2024/7/3 15:20
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    @Override
    public SysUser registers(SysUser sysUser) {
        sysUser.setName("小三");
        return null;
    }

    @Override
    public SysUserVo login(SysUserLoginDto dto) throws BizException {
        if (StringUtils.isEmpty(dto.getPhone()) || StringUtils.isEmpty(dto.getPwd())) {
            throw new BizException("手机号或密码不能为空");
        }

        LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getPhone, dto.getPhone());

        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        if (sysUser == null || !BCrypUtil.matches(dto.getPwd(), sysUser.getPwd())) {
            throw new BizException("登录失败");
        }

        // 生成 JWT 令牌
        String token = JwtUtil.generateToken(dto.getPhone()); // 1小时过期时间

        SysUserVo sysUserVo = new SysUserVo();
        BeanUtils.copyProperties(sysUser,sysUserVo);
//        sysUserVo.setId(sysUser.getId());
//        sysUserVo.setName(sysUser.getName());
//        sysUserVo.setPhone(sysUser.getPhone());
//        sysUserVo.setPwd(sysUser.getPwd());
//        sysUserVo.setAvatar(sysUser.getAvatar());
//        sysUserVo.setCreated(sysUser.getCreated());
//        sysUserVo.setUpdated(sysUser.getUpdated());
        sysUserVo.setJwt(token);

        return sysUserVo;
    }

    @Override
    public SysUser register(SysUserRegisterDto dto) throws BizException {
//        SysUser sysUser = new SysUser();

        // 检查必填字段
        if (StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPhone()) || StringUtils.isEmpty(dto.getPwd())) {
            throw new BizException("所有字段都是必填的");
        }
        // 检查用户是否已存在
        LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getPhone, dto.getPhone());

        SysUser existingUser = sysUserMapper.selectOne(wrapper);
        if (existingUser != null) {
            throw new BizException("该手机号已被注册");
        }
        // 创建新用户
//        dto.setPwd(BCrypUtil.encode(dto.getPwd())); // 密码加密优化
//        dto.setPwd(encoder.encode(dto.getPwd()));// 密码加密
        SysUser newUser = new SysUser();
        //工具类整体拷贝
        BeanUtils.copyProperties(dto,newUser);
        newUser.setPwd(BCrypUtil.encode(dto.getPwd()));//密码加密优化
        newUser.setCreated(new Date());//用户创佳时间
        newUser.setAvatar("/img/csgo.png");
        // 保存用户
        int result = sysUserMapper.insert(newUser);
        if (result != 1) {
            throw new BizException("注册失败，请重试");
        }

        return newUser;
    }
}
